 class ClassesController {
        def createBlogService
        def timeUtilService
        def authenticateService
        def teacherService
        def roleService
        

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        def c = Classes.createCriteria()
        if (authenticateService.isLoggedIn()) { 
            def classesInstanceList = c {
                eq("school", session.school)
                maxResults(10)
                order("level", "desc")
                order("grade", "asc")
                order("team", "asc")
            }
            [classesInstanceList: classesInstanceList, 
                classesInstanceTotal: Classes.countBySchool(session.school)]
        }else{
            def classesInstanceList = c {
                maxResults(10)
                order("level", "desc")
                order("grade", "asc")
                order("team", "asc")
            }
            [classesInstanceList: Classes.list(params), classesInstanceTotal: Classes.count()]
        }
    }

    def create = {
        def classesInstance = new Classes()
        classesInstance.properties = params
        return [classesInstance: classesInstance,peoples:teacherService.getSchoolMasterTeacher(session.school)]
    }

    def save = {
       

        // 由于前台用了jquery的日期插件，这里需要转换日期的格式
        Date buildTime = timeUtilService.stringToDate(params.buildTime)
        log.info(buildTime)
        params.buildTime = buildTime
        log.info(buildTime)
        def classesInstance = new Classes(params)
        classesInstance.school = session.school
        def results = Classes.withCriteria { 
                eq ('level',params.level)
                eq ('grade',params.grade.toInteger())
                eq ('team',params.team.toInteger())
                eq ('school',session.school)
        }
        if (results.size() > 0){
            flash.message = "已经存在了${params.level}${params.grade}年级${params.team.toInteger()+1}班"
            return render(view: "create", model: [classesInstance: classesInstance,peoples:teacherService.getSchoolMasterTeacher(session.school)])
        }
        if (classesInstance.save(flush: true)) {
//            roleService.addMasterRole(classesInstance.master)
            if(createBlogService.sendCreateClassesBlog(classesInstance,grailsApplication.config.grails.default.blog.create.uri)){
                flash.message = "创建班级${classesInstance}成功，同时建立了班级博客！"
            }else{
                flash.message = "创建班级${classesInstance}成功，创建班级博客失败，请在班级列表中再次进行创建，如果依然有错误，请联系管理员！"
            }
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [classesInstance: classesInstance,peoples:teacherService.getSchoolMasterTeacher(session.school)])
        }
    }

    def show = {
        def classesInstance = Classes.get(params.id)
        if (!classesInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'classes.label', default: 'Classes'), params.id])}"
            redirect(action: "list")
        }
        else {
            [classesInstance: classesInstance]
        }
    }

    def edit = {
        def classesInstance = Classes.get(params.id)
        if (!classesInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'classes.label', default: 'Classes'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [classesInstance: classesInstance,peoples:teacherService.getSchoolMasterTeacher(session.school)]
        }
    }

    def update = {
        def classesInstance = Classes.get(params.id)
        if (classesInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (classesInstance.version > version) {
                    
                    classesInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'classes.label', default: 'Classes')] as Object[], "Another user has updated this Classes while you were editing")
                    render(view: "edit", model: [classesInstance: classesInstance,,peoples:teacherService.getSchoolMasterTeacher(session.school)])
                    return
                }
            }
            // 由于前台用了jquery的日期插件，这里需要转换日期的格式
            Date buildTime = timeUtilService.stringToDate(params.buildTime)
            log.info(buildTime)
            params.buildTime = buildTime
            def oldMaster = classesInstance.master
            classesInstance.properties = params
            def results = Classes.withCriteria { 
                ne ('id',classesInstance.id)
                eq ('level',params.level)
                eq ('grade',params.grade.toInteger())
                eq ('team',params.team.toInteger())
                eq ('school',session.school)
            }
            if (results.size() > 0){
                flash.message = "已经存在了${params.level}${params.grade}年级${params.team.toInteger()+1}班"
                return render(view: "edit", model: [classesInstance: classesInstance,peoples:teacherService.getSchoolMasterTeacher(session.school)])
            }
            if (!classesInstance.hasErrors() && classesInstance.save(flush: true)) {
//                roleService.addMasterRole(classesInstance.master)
//                roleService.resetTeacher(oldMaster)
                if(createBlogService.sendCreateClassesBlog(classesInstance,grailsApplication.config.grails.default.blog.create.uri)){
                    flash.message = "更新班级${classesInstance}成功，更新班级博客的班主任成功！"
                }else{
                    flash.message = "更新班级${classesInstance}成功，更新班级博客的班主任失败，请在班级列表中再次进行更新，如果依然有错误，请联系管理员！"
                }
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [classesInstance: classesInstance,,peoples:teacherService.getSchoolMasterTeacher(session.school)])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'classes.label', default: 'Classes'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def classesInstance = Classes.get(params.id)
        if (classesInstance) {
            try {
                classesInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'classes.label', default: 'Classes'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'classes.label', default: 'Classes'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'classes.label', default: 'Classes'), params.id])}"
            redirect(action: "list")
        }
    }

}
